package cn.tedu.carehome.foundation.service.impl;

import cn.tedu.carehome.foundation.ex.ServiceException;
import cn.tedu.carehome.foundation.mapper.FoodMapperNew;
import cn.tedu.carehome.foundation.myPojo.foodPojoAuto.FoodNew;
import cn.tedu.carehome.foundation.pojo.dto.FoodUpdateDTO;
import cn.tedu.carehome.foundation.pojo.vo.FoodAdminVO;
import cn.tedu.carehome.foundation.pojo.vo.FoodStandardVO;
import cn.tedu.carehome.foundation.pojo.vo.FoodUpdateVO;
import cn.tedu.carehome.foundation.mapper.FoodMapper;
import cn.tedu.carehome.foundation.pojo.dto.FoodAddNewDTO;
import cn.tedu.carehome.foundation.pojo.entity.Food;
import cn.tedu.carehome.foundation.restful.JsonPage;
import cn.tedu.carehome.foundation.service.IFoodService;
import cn.tedu.carehome.foundation.web.ServiceCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class FoodServiceImpl implements IFoodService {
    @Autowired
    private FoodMapper foodMapper;

    @Autowired
    private FoodMapperNew foodMapperNew;

    @Override
    public void addNew(FoodAddNewDTO foodAddNewDTO){
        log.debug("开始处理【添加餐品】的业务，参数：{}", foodAddNewDTO);
        // 检查餐品名称是否已经被占用
        String name = foodAddNewDTO.getName();
        int countByName = foodMapper.countByName(name);
        if (countByName > 0) {
            // 如果已经被占用，抛出异常
            String message = "添加餐品失败，餐品名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        FoodNew foodNew = new FoodNew();
        BeanUtils.copyProperties(foodAddNewDTO,foodNew);
        log.debug("即将插入数据，参数：{}", foodNew);
        foodMapperNew.insert(foodNew);



    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【根据id删除商品】的业务，编号参数：{}", id);
        int countById= foodMapper.countById(id);

        //检查id是否存在
        {
            if (countById == 0) {
                String message = "删除餐品失败,餐品id不存在!";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
            }
        }

        //检查餐品是否关联了套餐
        {
            int foodPackageCount = foodMapper.countFoodPackageByFoodId(id);
            if (foodPackageCount > 0) {
                String message = "删除餐品失败,餐品已经与套餐绑定,请解除绑定后再删除!";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
            }
        }
        foodMapper.deleteById(id);
        log.warn("删除餐品成功");
    }

    @Override
    public List<FoodAdminVO> queryFoodList() {
        List<FoodAdminVO> list=foodMapper.selectAdmin();
        log.debug("开始处理【查询餐品列表】的业务，无参数");
        return list;
    }

    @Override
    public FoodStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据餐品id查询餐品】的业务，参数id{}",id);
        return foodMapper.getStandardById(id);
    }

    @Override
    public void update(FoodUpdateDTO foodUpdateDTO) {
        log.debug(String.valueOf(foodUpdateDTO));
        log.debug("开始处理【根据餐品id修改餐品】的业务，参数{}",foodUpdateDTO);
        // 检查餐品id是否存在
        {
            int countById = foodMapper.countById(foodUpdateDTO.getId());
            if (countById == 0) {
                String message = "修改餐品失败,餐品id不存在!";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
            }
        }



        //检查修改后的餐品名字是否和已有的餐品重名
        Long id=foodUpdateDTO.getId();
        String name=foodUpdateDTO.getName();
        int count= foodMapper.countByNameAndNotId(name,id);
        if(count>0){
            String message = "修改餐品失败,餐品的名字有其他餐品用过了!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        foodMapper.update(foodUpdateDTO);
    }

    @Override
    public JsonPage<FoodStandardVO> listFoods(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<FoodStandardVO> list=foodMapper.selectAdminByPage();
        return JsonPage.restPage(new PageInfo<>(list));
    }


}
